package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Menu;
import cn.itcast.service.MenuService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;



    /**
     * 查询所有菜单
     *
     * @return
     */
    @GetMapping
    public Result queryAll() {
        try {
            List<Menu> pageResult = this.menuService.queryAll();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_MENU_FAIL);
    }


    /**
     * 分页查询
     *
     * @param
     * @return
     */
    @GetMapping("/page")
    public Result queryByPage(QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = this.menuService.queryByPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_MENU_FAIL);
    }


    /**
     * 添加
     *
     * @param menu
     * @param permissionIds
     * @return
     */
    @PostMapping
    public Result saveMenu(@RequestBody Menu menu,
                           @RequestParam("permissionIds") Integer[] permissionIds) {

        try {
            this.menuService.saveMenu(menu, permissionIds);
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ADD_MENU_FAIL);
    }


    /**
     * 根据id查数据详情
     *
     * @param id
     * @return
     */
    @GetMapping(path = "/{id}")
    public Result queryMenu(@PathVariable Integer id) {

        try {
            Menu menu = this.menuService.queryById(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menu);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_MENU_FAIL);
    }

    //查询菜单管理权限信息
    @GetMapping("/{id}/permissions")
    public Result queryMenuPermission(@PathVariable Integer id) {
        try {
            Integer[] pageResult = this.menuService.queryMenuPermission(id);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
    }


    /**
     * 编辑
     *
     * @param menu
     * @param permissionIds
     * @return
     */
    @PutMapping
    public Result updateMenu(@RequestBody Menu menu,
                             @RequestParam("permissionIds") Integer[] permissionIds) {
        try {
            this.menuService.updateMenu(menu, permissionIds);
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new Result(false, MessageConstant.EDIT_MENU_FAIL);
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteMenu(@PathVariable Integer id) {
        Boolean flag = this.menuService.deleteById(id);
        if (flag) {
            return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
        }
        return new Result(false, MessageConstant.DELETE_MENU_FAIL);
    }

    @GetMapping("/getMenuList")
    public Result getMenuList(String username) {
        try {
            List<Menu> menuList = this.menuService.getMenuList(username);
            if (menuList != null) {
                return new Result(true, "菜单加载成功!", menuList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "菜单加载失败!");
    }

}
